package it.kion.util.ui.ulog2.preferences;

import it.kion.util.ui.ulog2.Filter;
import it.kion.util.ui.ulog2.Log4jPlugin;
import it.kion.util.ui.ulog2.Log4jUtil;

import java.util.StringTokenizer;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class Log4jFilterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	public static final String ID = "it.kion.util.ui.ulog2.preferences.Log4jFilterPreferencePage";

	public static final String P_FILTERS = "filters";

	private Combo combo1;

	private Combo combo2;

	private Text criteriaText;

	private List filterList;

	public Log4jFilterPreferencePage() {
	}

	public String[] convert(String input) {
		StringTokenizer st = new StringTokenizer(input, ";;;");
		int tokenCount = st.countTokens();
		String rval[] = new String[tokenCount];
		for (int i = 0; i < tokenCount; i++) {
			rval[i] = st.nextToken();
		}
		return rval;
	}

	protected Control createContents(Composite parent) {
		Composite entryTable = new Composite(parent, 0);
		GridData data = new GridData(768);
		data.grabExcessHorizontalSpace = true;
		entryTable.setLayoutData(data);
		GridLayout layout = new GridLayout();
		entryTable.setLayout(layout);
		new Label(entryTable, 0);
		filterList = new List(entryTable, 2048);
		filterList.setItems(convert(Log4jPlugin.getDefault().getPreferenceStore().getString("filters")));
		data = new GridData(1808);
		filterList.setLayoutData(data);
		data = new GridData(768);
		combo1 = new Combo(entryTable, 12);
		for (String s : Log4jPlugin.getMainView().getColumnHeaders()) {
			combo1.add(s);
		}
		combo1.select(0);
		combo1.setLayoutData(data);
		data = new GridData(768);
		combo2 = new Combo(entryTable, 12);
		combo2.add("includes");
		combo2.add("excludes");
		combo2.select(0);
		combo2.setLayoutData(data);
		data = new GridData(768);
		criteriaText = new Text(entryTable, 2048);
		criteriaText.setLayoutData(data);
		Button addButton = new Button(entryTable, 16777224);
		addButton.setText("Aggiungi alla lista");
		addButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent event) {
				String filter = combo1.getText() + " " + combo2.getText() + " "	+ criteriaText.getText();
				filterList.add(filter, filterList.getItemCount());
				Filter filtere = Filter.composeFromForm(filter);
				Log4jPlugin.getDefault().getLoggingModel().getFilterSet().addFilter(filtere);
				Log4jUtil.filterUpdated();
			}
		});
		
		Button removeButton = new Button(entryTable, 16777224);
		removeButton.setText("Rimuovi dalla lista");
		removeButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent event) {
				String selection = filterList.getSelection()[0];
				Filter filter = Filter.composeFromForm(selection);
				Log4jPlugin.getDefault().getLoggingModel().getFilterSet()
						.removeFilter(filter);
				filterList.remove(filterList.getSelectionIndex());
				Log4jUtil.filterUpdated();
			}
		});
		
		initializeDefaults();
		return entryTable;
	}

	public void createFieldEditors() {
	}

	public String[] getDefaultFilters() {
		return convert(getPreferenceStore().getDefaultString("filters"));
	}

	public void init(IWorkbench workbench) {
		setPreferenceStore(Log4jPlugin.getDefault().getPreferenceStore());
	}

	private void initializeDefaults() {
		for (Filter f : Log4jPlugin.getDefault().getLoggingModel().getFilterSet()) {
			filterList.add(f.toString());
		}
	}

}
